package com.lipiao.makerandroid.View.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.lipiao.makerandroid.MainActivity;
import com.lipiao.makerandroid.R;
import com.lipiao.makerandroid.Utils.HttpUtil;
import com.lipiao.makerandroid.View.Fragment.MainFragment;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class AddActivity extends AppCompatActivity {
    static final String TAG = "AddActivity";

    String userNumber;

    EditText editText;

    private MainFragment mainFragment;//我的
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_read);
        editText = findViewById(R.id.tv_top_article_title);
        userNumber = getIntent().getStringExtra("userNumber");
    }
    public void addRead(View view){
        if(editText.getText().toString().equals("")){
            Toast.makeText(this,"内容不能为空！",Toast.LENGTH_SHORT).show();
        }else {
            String text = editText.getText().toString();
            Call<ResponseBody> call = HttpUtil.getUserService().addRead(userNumber,text);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    String strBack = null;
                    try {
                        strBack = response.body().string();
                        if(strBack.equals("addSuccess")){
                            Toast.makeText(getBaseContext(),"发布成功！",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getBaseContext(), MainActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(getBaseContext(),"发布失败！",Toast.LENGTH_SHORT).show();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });
        }
    }

    public void back(View view) {
        onBackPressed();
    }
}
